﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GerbilLib;

namespace MediaGerbil
{
    public partial class TitleMatches : Form
    {
        private MediaItem Item { get; set; }
        private List<IMediaItem> ProposedMatches { get; set; }

        public TitleMatches()
        {
            InitializeComponent();
        }

        public TitleMatches(MediaItem item, List<IMediaItem> proposedMatches)
            : this()
        {
            Item = item;
            ProposedMatches = proposedMatches;
            InitializeForm();
        }

        private void InitializeForm()
        {
            lblTitle.Text = Item.Title;
            foreach (IMediaItem match in ProposedMatches)
                lstMatches.Items.Add(match);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            IMediaItem selectedItem = lstMatches.SelectedItem as IMediaItem;
            if (selectedItem != null)
            {
                Item.Title = selectedItem.Title;
                if (selectedItem is MediaItem) Item.ImdbId = ((MediaItem)selectedItem).ImdbId;
            }
            Close();
        }

        private void lstMatches_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstMatches.SelectedIndex != -1)
            {
                IMediaItem selectedItem = lstMatches.SelectedItem as IMediaItem;
                if (selectedItem == null || selectedItem as MediaItem == null) return;
                if (string.IsNullOrEmpty(((MediaItem)selectedItem).GetThumbnailImageUrl()))
                    coverImagePictureBox.Image = null;
                else
                    coverImagePictureBox.ImageLocation = ((MediaItem)selectedItem).GetThumbnailImageUrl();
            }
        }

        private void lstMatches_DoubleClick(object sender, EventArgs e)
        {
            btnOk_Click(sender, e);
        }
    }
}
